package com.sdleyou.treepro.business.api.message;


import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.service.UserMessageManager;
import com.sdleyou.treepro.system.version.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api")
public class MessageController {
    @Autowired
    private UserMessageManager userMessageManager;

    /**
     * 获取消息列表
     *
     * @param userId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/messages")
    public ResultVO list(@RequestParam("user_id") @NotEmpty(message = "user_id不能为空") String userId,
                         @RequestParam(name = "page_size", defaultValue = "5") Integer pageSize,
                         @RequestParam(name = "page_num", defaultValue = "1") Integer pageNum) {
        return ResultVOUtil.success(userMessageManager.list(pageSize, pageNum, userId));
    }

    /**
     * 设置为已读
     *
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/message/read")
    public ResultVO read(@RequestParam("message_id") @NotEmpty(message = "message_id不能为空") String messageId) {
        userMessageManager.read(messageId);
        return ResultVOUtil.success();
    }

    /**
     * 删除消息
     *
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/message/delete")
    public ResultVO delete(@RequestParam("message_id") @NotEmpty(message = "message_id不能为空") String messageId) {
        userMessageManager.delete(messageId);
        return ResultVOUtil.success();
    }
}
